package com.example.callmanagerandroid;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.database.Contact;

public class phoneRing extends Activity {

	
	int flag = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_alert);
		String number = "";
		number = getIntent().getStringExtra("number");
		String id = "";
		id = getIntent().getStringExtra("id");
		String ten = "";
		ten = getIntent().getStringExtra("ten");
		String dt = "";
		dt = getIntent().getStringExtra("dt");
		String dc = "";
		dc = getIntent().getStringExtra("dc");
		String gc = "";
		gc = getIntent().getStringExtra("gc");
		
		flag = getIntent().getIntExtra("flag", 1);
		 
		final String content = "-Số gọi đến : "+number+
				"\n-Mã khách : "+id+
				"\n-Tên khách : "+ten+
				"\n-Địa chỉ : "+dc+
				"\n-Chú giải : "+gc;

		TextView numbers = (TextView) findViewById(R.id.tvphone);
		numbers.setText(number);
		TextView tid = (TextView) findViewById(R.id.tvID);
		tid.setText(id);
		TextView ttk = (TextView) findViewById(R.id.tvName);
		ttk.setText(ten);
		TextView tdc = (TextView) findViewById(R.id.tvAddr);
		tdc.setText(dc);
		TextView tgc = (TextView) findViewById(R.id.tvGhiChu);
		tgc.setText(gc);

		Button dialogButton = (Button) findViewById(R.id.button1);
		dialogButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		Button sendMess = (Button) findViewById(R.id.button2);
		Button traLoi = (Button) findViewById(R.id.button3);
		if(flag == 1){
			sendMess.setVisibility(View.GONE);
		}else{
			traLoi.setVisibility(View.GONE);
		}
		sendMess.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				sendSMS(content,getNumber());
				showDialog(content);
			}
		});
		
		
		traLoi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
				i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK));
				sendOrderedBroadcast(i, null); 
			}
		});
	}
	
	
	
	private void sendSMS(String content, String number){
		Log.w("","###"+number);
		Uri uri = Uri.parse(number);   
		Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
		it.putExtra("sms_body", content);   
		startActivity(it);
	}
	
	private List<String> getNumber(){
		List<String> list = new ArrayList<String>();
		String str_number = "smsto:";
		Contact contact = new Contact(this);
		Cursor c = contact.selectPhone();
		if(c!=null)
			if(c.moveToFirst()){
				do {
					String number = c.getString(c.getColumnIndexOrThrow("number"));
					list.add(number);
				} while (c.moveToNext());
			}
		c = null;
		contact.close();
		contact = null;
		return list;
	}
	
	private String getNumberString(){
		String str_number = "smsto:";
		Contact contact = new Contact(this);
		Cursor c = contact.selectPhone();
		if(c!=null)
			if(c.moveToFirst()){
				do {
					String number = c.getString(c.getColumnIndexOrThrow("number"));
					str_number+=number;
				} while (c.moveToNext());
			}
		c = null;
		contact.close();
		contact = null;
		return str_number;
	}
	
	private void showDialog(final String content){
		final Dialog dialog = new Dialog(phoneRing.this);
		dialog.setContentView(R.layout.call_list);
		LinearLayout liner = (LinearLayout) dialog.findViewById(R.id.layout);
		liner.setVisibility(View.GONE);
		
		ListView list = (ListView) dialog.findViewById(R.id.listView1);
		list.setAdapter(new ChoisePhoneNumberAdapter(getNumber(), phoneRing.this));
		dialog.setTitle("Chọn số gởi đến");
		
		Button thoat = (Button)dialog.findViewById(R.id.button3);
		Button goi = (Button)dialog.findViewById(R.id.button2);
		
		thoat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		goi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				final SharedPreferences sharedpreferences = getSharedPreferences("sharethucoi", Context.MODE_PRIVATE);
				String str = "smsto:";
				List<String> arrN = getNumber();
				for(int i=0;i< arrN.size();i++){
					if(sharedpreferences.getBoolean("key"+String.valueOf(i), false)){
						str += arrN.get(i)+",";
					}
				}
				sendSMS(content,str);
			}
		});

		dialog.show();
	}

}
